<?php

namespace Beerus\Modules\Empowerment\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Infinity\service\empowerment\Reservation as sReservation;

class ReservationController
{
    public function index(Content $content, Request $request)
    {
        $requestParams = $request->all();
        $condition = $this->_index_condition($requestParams);
        $pageSize = $request->input("per_page", 10);
        $page = $request->input("page", 1);
        $condition['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $listAndTotal = sReservation::admGetList($condition);
        $gridModel = new pGridDataModel($listAndTotal['list'], $listAndTotal['total']);
        $listGrid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $condition) {
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function (Grid\Filter $filter) use ($condition) {
                    $filter->disableIdFilter();

                    $filter->column(4, function ($filter) {
                        $filter->dropDown('designer', [
                            'designer_uid' => '设计师uid',
                            'designer_nick' => '设计师昵称',
                        ]);
                    });
                    $filter->column(6, function ($filter) {
                        $filter->between('date_time', '创建时间')->datetime()->condition($_GET);
                    });
                    if (!empty($condition)) {
                        $filter->expand();//默认展开
                    }
                });
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->disableColumnSelector();
                $grid->column('id', 'id');
                $grid->column('designer_uid', '设计师uid');
                $grid->column('designer_nick', '设计师昵称');
                $grid->column('uid', '预约用户uid');
                $grid->column('nick', '预约用户');
                $grid->column('addtime', '预约时间');
                $grid->column('', '装修信息')->display(function () {
                    $html = '<dl class="dl-horizontal"><dt>房屋所在地</dt><dd>' . $this->area . '</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>手机号</dt><dd>' . $this->phone . '</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>建筑面积</dt><dd>' . $this->space . '平方</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>户型</dt><dd>' . $this->house_type . '</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>房屋状态</dt><dd>' . $this->house_status . '</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>预算</dt><dd>' . $this->budget . '（万元）</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>开始装修时间</dt><dd>' . $this->start_time . '</dd></dl>';
                    $html .= '<dl class="dl-horizontal"><dt>计划入住时间</dt><dd>' . $this->check_time . '</dd></dl>';
                    return $html;
                });
            });

        return $content->row($listGrid->render())->breadcrumb(
            ['text' => '找设计师', 'url' => '/empowerment/reservation'],
            ['text' => '预约列表']
        );;
    }

    public function _index_condition($request)
    {
        $response = [];
        //商品名称条件
        if (isset($request['designer_nick'])) {
            $designerNick = trim($request['designer_nick']);
            if (strlen($designerNick)) {
                $uid = User::getUidByNick($designerNick);
                if (!empty($uid)) {
                    $response['designer_uid'] = $uid;
                } else {
                    $response['designer_uid'] = 0;
                }
            }
        }
        if (isset($request['designer_uid']) && ($request['designer_uid'] > 0)) {
            $response['designer_uid'] = $request['designer_uid'];
        }
        //创建时间条件
        if (isset($request['date_time']['start']) && strlen($request['date_time']['start'])) {
            $response['addtime[>=]'] = $request['date_time']['start'];
        }
        if (isset($request['date_time']['end']) && strlen($request['date_time']['end'])) {
            $response['addtime[<=]'] = $request['date_time']['end'];
        }
        return $response;
    }

}
